﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Medical.Business;

/// <summary>
/// 友情链接列表
/// </summary>
public partial class webadmin_LinkList : AdminPage
{
    public int nType = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.Site;
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitData()
    {
        nType = Convert.ToInt32(ddlSort.SelectedValue);
        DataTable dtLink = new BisLinkInfo().GetLinkInfoList(nType);
        if (dtLink.Rows.Count == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptLink.DataSource = dtLink;
        rptLink.DataBind();
    }

    /// <summary>
    /// 获取名称
    /// </summary>
    /// <param name="eType"></param>
    /// <returns></returns>
    public string GetName(int nType)
    {
        switch (nType)
        {
            case 1:
                return "医疗机构";
            case 2:
                return "热门标签";
            case 3:
                return "友情链接";
        }
        return "";
    }


    protected string CheckDisplay()
    {
        if (nType!=1)
            return "display:none";
        return "";
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }
    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        nType = Convert.ToInt32(ddlSort.SelectedValue);
        InitData();
    }

    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptSort_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        ImageButton btnUp = e.Item.FindControl("btnUp") as ImageButton;
        ImageButton btnDown = e.Item.FindControl("btnDown") as ImageButton;
        int nItemID = e.Item.ItemIndex;
        int nCount = dtSort.Rows.Count;
        //上移
        if (nItemID<(nCount-1))
        {
            btnDown.CommandArgument = dtSort.Rows[nItemID]["ID"].ToString() + ","
                                    + dtSort.Rows[nItemID + 1]["OrderNum"].ToString() + ","
                                    + dtSort.Rows[nItemID + 1]["ID"].ToString() + ","
                                    + dtSort.Rows[nItemID]["OrderNum"].ToString();
        }
        //下移
        if (nItemID>0)
        {
            btnUp.CommandArgument = dtSort.Rows[nItemID]["ID"].ToString() + ","
                                  + dtSort.Rows[nItemID - 1]["OrderNum"].ToString() + ","
                                  + dtSort.Rows[nItemID - 1]["ID"].ToString() + ","
                                  + dtSort.Rows[nItemID]["OrderNum"].ToString();
        }
        if (nItemID == 0)
        {
            //禁用第一个上移按钮
            btnUp.Enabled = false;
            btnUp.ImageUrl = "images/noup.gif";
            btnUp.CommandArgument = "";

        }
        if (nItemID == (nCount - 1))
        {
            //禁用最后一个下移按键
            btnDown.Enabled = false;
            btnDown.ImageUrl = "images/nodown.gif";
            btnDown.CommandArgument = "";
        }
    }

     /// <summary>
    /// 分类列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptLink_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        BisLinkInfo bisLinkInfo = new BisLinkInfo();
        bool result = false;
        if (e.CommandName == "OrderUp" || e.CommandName == "OrderDown")
        {
            if (e.CommandArgument.ToString() != string.Empty)
            {
                //上移下移操作
                string[] strItem = e.CommandArgument.ToString().Split(',');
                result = bisLinkInfo.ChangeOrder(
                                    Convert.ToInt32(strItem[0]),
                                    Convert.ToInt32(strItem[1]),
                                    Convert.ToInt32(strItem[2]),
                                    Convert.ToInt32(strItem[3]));
                if (!result)
                    Until.Alert("操作失败！");
            }

        }
        else if (e.CommandName == "Delete")
        {
            //删除信息
            string[] strItem = e.CommandArgument.ToString().Split(',');
            result = bisLinkInfo.Delete(Convert.ToInt32(strItem[0]));
            if (result)
                Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + strItem[1]));
            Until.Alert(result ? "删除成功！" : "删除失败！");
        }

        //重新加载
        if (result)
        {
            InitData();
        }
    }
}